package com.ai.enviro.common;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.Data;

@Data
public class BaseResponse<T> {
    private static final long serialVersionUID = 1L;

    private String code;

    private String msg;

    private T data;

    public BaseResponse() {
    }

    public BaseResponse(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public BaseResponse(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> BaseResponse<T> success(T data) {
        return new BaseResponse<>(CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMsg(), data);
    }

    public static <T> BaseResponse<T> successMsg(String msg) {
        if (StringUtils.isBlank(msg)) {
            msg = CodeEnum.SUCCESS.getMsg();
        }
        return new BaseResponse<>(CodeEnum.SUCCESS.getCode(), msg);
    }


    public static <T> BaseResponse<T> failed(String message) {
        if (StringUtils.isBlank(message)) {
            message = CodeEnum.FAILED.getMsg();
        }
        return new BaseResponse<>(CodeEnum.FAILED.getCode(), message);
    }

    public static <T> BaseResponse<T> failed(CodeEnum status, String moreInfo) {
        if (moreInfo == null) {
            moreInfo = "";
        }
        return new BaseResponse<>(status.getCode(), String.format("%s[%s]", status.getMsg(), moreInfo));
    }
}
